package com.gfxy.modules.items.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gfxy.modules.common.entity.MemberList;
import com.gfxy.modules.common.entity.MemberSimpleList;
import com.gfxy.modules.common.service.IMemberListService;
import com.gfxy.modules.items.entity.Cjrh;
import com.gfxy.modules.items.service.DictTranslater;
import com.gfxy.modules.items.service.ICjrhService;


import com.gfxy.modules.items.vo.CjrhMemberPage;
import com.gfxy.modules.items.vo.CjrhPage;
import com.gfxy.modules.excel.TemplateUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: gfxy_t_cjrh
 * @Author: jeecg-boot
 * @Date:   2023-03-24
 * @Version: V1.0
 */
@Api(tags="产教融合")
@RestController
@RequestMapping("/teaching/cjrh")
@Slf4j
public class CjrhController {
	@Autowired
	private ICjrhService cjrhService;

	 @Autowired
	 private IMemberListService memberListService;

	 @Autowired
	 private DictTranslater dictTranslater;

	 @Autowired
	 private TemplateUtil templateUtil;
	
	/**
	 * 分页列表查询
	 *
	 * @param
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "gfxy_t_cjrh-分页列表查询")
	@ApiOperation(value="gfxy_t_cjrh-分页列表查询", notes="gfxy_t_cjrh-分页列表查询")
	@GetMapping(value = "/listByYear")
	public Result<?> queryPageList( @RequestParam(name="date")  String date,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		IPage<CjrhMemberPage> pageList = cjrhService.selectListPage(date,pageNo,pageSize);
		return Result.OK(pageList);
	}


	 @AutoLog(value = "教学成果奖-获取总分")
	 @ApiOperation(value="教学成果奖-获取总分", notes="教学成果奖-获取总分")
	 @GetMapping(value = "/getTotalScore")
	 public Result<?> getTotalScore(@RequestParam(name="cjrhType", defaultValue="0")  Integer cjrhType,
									@RequestParam(name="cjrhStatus", defaultValue="0") Integer cjrhStatus,
									HttpServletRequest req) {
		 Cjrh cjrh = new Cjrh();
		 cjrh.setCjrhType(cjrhType);
		 cjrh.setCjrhStatus(cjrhStatus);

		 Double result = this.cjrhService.getTotalScore(cjrh);
		 return Result.OK(result);
	 }
	
	/**
	 *   添加
	 *
	 * @param
	 * @return
	 */
	@AutoLog(value = "gfxy_t_cjrh-添加")
	@ApiOperation(value="gfxy_t_cjrh-添加", notes="gfxy_t_cjrh-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CjrhPage cjrhPage) {
		Cjrh cjrh = new Cjrh();
		BeanUtils.copyProperties(cjrhPage, cjrh);
		cjrhService.saveMain(cjrh, cjrhPage.getMemberListList());
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param cjrhPage
	 * @return
	 */
	@AutoLog(value = "gfxy_t_cjrh-编辑")
	@ApiOperation(value="gfxy_t_cjrh-编辑", notes="gfxy_t_cjrh-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CjrhPage cjrhPage) {
		Cjrh cjrh = new Cjrh();
		BeanUtils.copyProperties(cjrhPage, cjrh);
		Cjrh cjrhEntity = cjrhService.getById(cjrh.getId());
		if(cjrhEntity==null) {
			return Result.error("未找到对应数据");
		}
		cjrhService.updateMain(cjrh, cjrhPage.getMemberListList());
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "gfxy_t_cjrh-通过id删除")
	@ApiOperation(value="gfxy_t_cjrh-通过id删除", notes="gfxy_t_cjrh-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		cjrhService.delMain(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "gfxy_t_cjrh-批量删除")
	@ApiOperation(value="gfxy_t_cjrh-批量删除", notes="gfxy_t_cjrh-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.cjrhService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "gfxy_t_cjrh-通过id查询")
	@ApiOperation(value="gfxy_t_cjrh-通过id查询", notes="gfxy_t_cjrh-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Cjrh Cjrh = cjrhService.getById(id);
		if(Cjrh==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(Cjrh);

	}

	 @AutoLog(value = "成员列表通过主表ID查询")
	 @ApiOperation(value="成员列表主表ID查询", notes="成员列表-通主表ID查询")
	 @GetMapping(value = "/queryMemberListByMainId")
	 public Result<?> queryMemberListListByMainId(@RequestParam(name="id",required=true) String id) {
		 List<MemberList> memberListList = memberListService.selectByItemId(id);
		 return Result.OK(memberListList);
	 }
	

    /**
    * 导出excel
    *
    * @param request
    * @param cjrh
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Cjrh cjrh)  throws NoSuchFieldException {
      try{
		  String date = cjrh.getDate();
		  List<CjrhMemberPage> cjrhMemberPages = cjrhService.getCjrhMemberPageListByYear(date);
		  List<String> title = new ArrayList<>();
		  title.add("时间");
		  title.add("项目名称");
		  title.add("项目类型");
		  title.add("状态");
		  title.add("获奖级别");
		  title.add("成员");
		  List<List<String>> contents = new ArrayList<>();
		  for (CjrhMemberPage cjrhMemberPage : cjrhMemberPages) {
			  List<String> content = new ArrayList<>();
			  content.add(cjrhMemberPage.getDate());
			  content.add(cjrhMemberPage.getProjectName());
			  content.add(dictTranslater.getDictValue(CjrhMemberPage.class, "cjrhType", String.valueOf(cjrhMemberPage.getCjrhType())));
			  if(cjrhMemberPage.getCjrhType() == 1){

				  content.add(cjrhService.getCyxyName(cjrhMemberPage.getCjrhStatus()));
				  content.add("");
			  } else if(cjrhMemberPage.getCjrhType() == 2){

				  content.add(cjrhService.getDdbName(cjrhMemberPage.getCjrhStatus()));
				  content.add("");
			  } else if(cjrhMemberPage.getCjrhType() == 3){

				  content.add("");
				  content.add(cjrhService.getOtherName(cjrhMemberPage.getCjrhStatus()));
			  }


			  List<MemberSimpleList> list = cjrhMemberPage.getMemberListList();
			  StringBuffer names = new StringBuffer();
			  for (MemberSimpleList memberSimpleList : list) {
				  names.append(memberSimpleList.getUserName()).append("、");
			  }
			  content.add(names.toString());
			  contents.add(content);
		  }
		  ModelAndView mv = new ModelAndView(new TeachingExcelView());
		  mv.addObject(TeachingExcelView.FILE_NAME,"产教融合-" + date);
		  mv.addObject(TeachingExcelView.TITLE, title);
		  mv.addObject(TeachingExcelView.CONTENT, contents);
		  return mv;
	  } catch (Exception e){
		  throw e;
	  }

    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(1);
			params.setNeedSave(true);
			try {

				return Result.OK(cjrhService.importExcel(file));
			} catch (Exception e) {
				log.error(e.getMessage(),e);
				return Result.error("文件导入失败:"+e.getMessage());
			} finally {
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return Result.OK("文件导入失败！");
    }

	 @GetMapping(value = "/handleExportXlsTemplate")
	 public Result<?> download(HttpServletRequest request, HttpServletResponse response)  {

		 try {
			 templateUtil.template(response,TemplateUtil.CJRH);
		 } catch (IOException e) {
			 e.printStackTrace();
			 return  Result.error("下载失败");
		 }
		 return  Result.OK("下载成功");
	 }

}
